package com.junzhiit.nengji.cms.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bainaeco.framework.util.DateUtil;
import com.junzhiit.nengji.bo.PayHistoryBO;
import com.junzhiit.nengji.cms.service.PayHistoryService;
import com.junzhiit.nengji.cms.web.exception.ShopNotFoundException;
import com.junzhiit.nengji.cms.web.exception.UserNotFoundException;
import com.junzhiit.nengji.cms.web.util.ViewObjectUtil;
import com.junzhiit.nengji.cms.web.vo.DataTablesParamVO;
import com.junzhiit.nengji.cms.web.vo.PayHistoryVO;
import com.junzhiit.nengji.cms.web.vo.SystemUserSessionVO;
import com.junzhiit.nengji.dao.ChainDAO;
import com.junzhiit.nengji.dao.PayHistoryDAO;
import com.junzhiit.nengji.dao.ShopDAO;
import com.junzhiit.nengji.dao.UserDAO;
import com.junzhiit.nengji.dao.cache.SystemUserCacheDAO;
import com.junzhiit.nengji.dao.util.DIYKey;
import com.junzhiit.nengji.model.Chain;
import com.junzhiit.nengji.model.Shop;


@Service
public class PayHistoryServiceImpl extends BaseServiceImpl implements PayHistoryService{
	
	@Autowired
	private PayHistoryDAO payHistoryDAO;
	
	@Autowired
	private ShopDAO shopDAO;
	
	@Autowired
	private UserDAO userDAO;
	
	@Autowired
	private SystemUserCacheDAO systemUserCacheDAO;
	
	@Autowired
	private ChainDAO chainDAO;

	@Override
	public Map<String, Object> queryPayHistory(DataTablesParamVO dtp,PayHistoryVO phv) {
		SystemUserSessionVO sessionVO=systemUserCacheDAO.getHash(DIYKey.getSessionHashKey(phv.getSessionId()), SystemUserSessionVO.class);
		String systemId=sessionVO.getSystemId();
		Shop shop=shopDAO.query(systemId);
		Chain chain=chainDAO.query(systemId);
		if(shop==null&&chain==null){
			throw new ShopNotFoundException("店铺/品牌不存在");
		}
		PayHistoryBO phb=new PayHistoryBO();
		BeanUtils.copyProperties(phv, phb);
		phb.setStartTime(DateUtil.getDateStart(DateUtil.parseDate(phv.getStartTime())));
		phb.setEndTime(DateUtil.getDateEnd(DateUtil.parseDate(phv.getEndTime())));
		if(shop!=null){
			phb.setShopId(shop.getId());
		}
		if(chain!=null){
			phb.setChainId(chain.getId());
		}
		List<PayHistoryBO> list=payHistoryDAO.queryPayHistory(phb,dtp.getStart(),dtp.getLength());
		Integer totalCount=payHistoryDAO.getTotalCount(phb);
		return ViewObjectUtil.dataTables(list, dtp.getDraw(), totalCount);
	}

	@Override
	public Map<String, Object> queryPayRanking(DataTablesParamVO dtp,PayHistoryVO payHistoryVO) {
		SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO
				.getHash(DIYKey.getSessionHashKey(payHistoryVO.getSessionId()), SystemUserSessionVO.class);
		Shop shop = shopDAO.query(systemUserSessionVO.getSystemId());
		if (shop == null) {
			throw new ShopNotFoundException("店铺不存在");
		}
		PayHistoryBO payHistoryBO=new PayHistoryBO();
		BeanUtils.copyProperties(payHistoryVO, payHistoryBO);
		payHistoryBO.setShopId(shop.getId());
		payHistoryBO.setStartTime(DateUtil.getDateStart(DateUtil.parseDate(payHistoryVO.getStartTime())));
		payHistoryBO.setEndTime(DateUtil.getDateEnd(DateUtil.parseDate(payHistoryVO.getEndTime())));
		
		List<PayHistoryBO> list=payHistoryDAO.queryPayRanking(payHistoryBO,dtp.getStart(),dtp.getLength());
		Integer totalCount=payHistoryDAO.getPayRankingTotalCount(payHistoryBO);
		
		return ViewObjectUtil.dataTables(list, dtp.getDraw(), totalCount);
	}

	@Override
	public Map<String, Object> queryUserPayHistory(DataTablesParamVO dtp,String sessionId,String userId) {
		SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO
				.getHash(DIYKey.getSessionHashKey(sessionId), SystemUserSessionVO.class);
		Shop shop = shopDAO.query(systemUserSessionVO.getSystemId());
		if (shop == null) {
			throw new ShopNotFoundException("店铺不存在");
		}
		if(userDAO.findUserId(userId) == null){
			throw new UserNotFoundException("该用户不存在");
		}
		List<PayHistoryBO> list= payHistoryDAO.queryUserPayHistory(shop.getId(), userId,dtp.getStart(),dtp.getLength());
		Integer totalCount=payHistoryDAO.getUserPayHistoryTotalCount(shop.getId(), userId);
		return ViewObjectUtil.dataTables(list, dtp.getDraw(), totalCount);
	}
}
